<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="抬头类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择" clearable size="small">
          <el-option label="全部" value="" />
          <el-option label="企业单位" value="1" />
          <el-option label="个人" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="发票抬头" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入发票抬头"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="税号" prop="registerno">
        <el-input
          v-model="queryParams.registerno"
          placeholder="请输入税号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="收件人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入收件人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-tabs v-model="tabActiveName"  @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="all"></el-tab-pane>
      <el-tab-pane label="待开票" name="1"></el-tab-pane>
      <el-tab-pane label="已开票" name="2"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" :data="invoiceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"  />
      <el-table-column label="订单编号" align="center" width="170" prop="num">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleOrderShow(scope.row)"
          >{{scope.row.num}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="抬头类型" align="left" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type == 1">企业单位</el-tag>
          <el-tag v-else type="success">个人</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发票抬头" align="left" prop="title" show-overflow-tooltip width="200" />
      <el-table-column label="税号" align="left" prop="registerno" width="200"  />
      <el-table-column label="发票内容" align="left" prop="content" />
      <el-table-column label="发票金额" align="center" prop="amount" />
      <el-table-column label="开户行" align="left" prop="bankName" >
        <template slot-scope="scope">
          <div>{{scope.row.bankName}}</div>
          <div>{{scope.row.cardNo}}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="卡号" align="center" prop="cardNo" /> -->
      <el-table-column label="公司电话" align="center" prop="telephone" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="收件人" align="left" prop="userName" width="120" >
        <template slot-scope="scope">
          <div>{{scope.row.userName}}</div>
          <div>{{scope.row.phonenumber}}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="手机号" align="center" prop="phonenumber" /> -->
      <el-table-column label="邮箱" align="center" prop="email"  width="150"/>
      <el-table-column label="申请时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1">待开票</span>
          <span v-else >已开票</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"  style="color: #f56c6c;"
            type="text" v-if="scope.row.status == 1"
            icon="el-icon-coordinate"
            @click="handleApprove(scope.row)"
          >开票</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleShow(scope.row)"
          >查看</el-button> -->
          <el-button
            size="mini"
            type="text"  v-if="scope.row.status == 2"
            icon="el-icon-search"
            @click="handleView(scope.row)"
          >预览</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电子发票对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="1:企业单位，2：个人" prop="type">
          <el-select v-model="form.type" placeholder="请选择1:企业单位，2：个人">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="发票抬头" prop="title">
          <el-input v-model="form.title" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="税号" prop="registerno">
          <el-input v-model="form.registerno" placeholder="请输入税号" />
        </el-form-item>
        <el-form-item label="发票内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发票金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入发票金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="电子发票地址" prop="fileUrl">
          <el-input v-model="form.fileUrl" placeholder="请输入电子发票地址" />
        </el-form-item>
        <el-form-item label="收件人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入收件人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="订单ids" prop="orderIds">
          <el-input v-model="form.orderIds" placeholder="请输入订单ids" />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="showOpen" width="50%" >
      <el-form ref="form" :model="orderForm" :rules="rules" label-width="100px" :disabled="true">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单编号" prop="num">
              <el-input v-model="orderForm.num" placeholder="请选择服务" :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单分类" prop="categoryName">
              <el-input v-model="orderForm.categoryName" placeholder="请选择服务" :readonly="true" >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="订单信息" prop="attribute">
              <el-input  v-model="orderForm.attribute" placeholder="请输入订单信息" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格属性" prop="tags">
              <el-input v-model="orderForm.tags" placeholder="规格属性" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="预约时间" prop="orderTime">
              <el-date-picker clearable size="small"
                              v-model="orderForm.orderTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择预约时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="服务地址" prop="address">
              <el-input v-model="orderForm.address" placeholder="请选择地址" :readonly="true"  >
                <el-button slot="append" icon="el-icon-search"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图片" prop="descImgUrl">
              <el-image v-if="orderForm.descImgUrl"
                        style="width: 80px; height: auto"
                        :src="orderForm.descImgUrl" z-index="9999"
                        :preview-src-list="[orderForm.descImgUrl]">
              </el-image>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-row :gutter="0">
              <el-col :span="12">
                <el-form-item label="订单单价" prop="price">
                  <el-input v-model="orderForm.price" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="订单数量" prop="number">
                  <el-input v-model="orderForm.number"  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <el-row :gutter="0">
              <el-col :span="12">
                <el-form-item label="应付金额" prop="payable">
                  <el-input v-model="orderForm.payable" placeholder="请输入应付" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="实付金额" prop="pay">
                  <el-input v-model="orderForm.pay" placeholder="请输入实付" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="detail">
              <el-input type="textarea" :rows="2" v-model="orderForm.detail" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listInvoice, getInvoice, delInvoice, addInvoice, updateInvoice, exportInvoice,publishInvoice } from "@/api/user/invoice";
import Editor from '@/components/Editor';
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder, cancelOrder } from "@/api/business/order";

export default {
  name: "Invoice",
  components: {
    Editor,
  },
  data() {
    return {
      tabActiveName: 'all',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电子发票表格数据
      invoiceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      showOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        title: null,
        registerno: null,
        content: null,
        amount: null,
        fileUrl: null,
        userName: null,
        phonenumber: null,
        email: null,
        orderIds: null,
        status: null
      },
      // 表单参数
      form: {},
      orderForm: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    handleOrderShow(row){
      this.showOpen = true;
      this.getOrder(row)
    },
    getOrder(row){
      const id = row.orderIds
      getOrder(id).then(response => {
        this.orderForm = response.data;
        this.orderForm.address = response.data.orderAddress.detail;
      });
    },

    out(){
      this.showOpen = false;
    },

    /** 查询电子发票列表 */
    getList() {
      this.loading = true;
      listInvoice(this.queryParams).then(response => {
        this.invoiceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        title: null,
        registerno: null,
        content: null,
        amount: null,
        remark: null,
        fileUrl: null,
        userName: null,
        phonenumber: null,
        email: null,
        createTime: null,
        orderIds: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleTabClick(tab){
      if (tab.name != 'all') {
        this.queryParams.status = tab.name;
      }else{
        this.queryParams.status = '';
      }
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电子发票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInvoice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改电子发票";
      });
    },
    handleApprove(row) {
      const id = row.id
      this.$confirm('是否确认开电子发票?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return publishInvoice(id);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInvoice(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoice(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除电子发票编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInvoice(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有电子发票数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInvoice(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        })
    }
  }
};
</script>
